package com.lyk.read.book.base

import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.lyk.common.base.BaseActivity
import com.lyk.common.util.ResourcesUtil
import com.lyk.read.book.view.PlaceView

/**
 * Created by LianYongKe on 2024/3/15.
 */
abstract class BaseBookActivity : BaseActivity() {

    private var mMyPlaceView: PlaceView? = null
    private var mMyContentView: View? = null

    override fun setPlaceView(view: View) {
        if (mMyPlaceView == null) {
            mMyPlaceView = PlaceView(this)
        }

        if (mMyPlaceView?.parent != null) {
            (mMyPlaceView?.parent as ViewGroup).removeView(mMyPlaceView)
        }

        when (val group: ViewGroup = view.parent as ViewGroup) {
            is LinearLayout -> {
                mMyContentView = view
                group.addView(mMyPlaceView, group.indexOfChild(view) + 1, view.layoutParams)
            }

            is ConstraintLayout -> {
                mMyContentView = null
                val layoutParams =
                    ResourcesUtil.copyConstraintLayoutParams(view.layoutParams as ConstraintLayout.LayoutParams)
                group.addView(mMyPlaceView, layoutParams)
            }

            else -> {
                mMyContentView = null
                group.addView(mMyPlaceView, view.layoutParams)
            }
        }
    }

    /**
     * 1. loading
     * 2. 空页面
     */
    override fun showPlaceView(type: Int) {
        mMyPlaceView?.setPlaceType(type, mMyContentView)
    }

}